package com.shenyi.shop.service;

import com.shenyi.core.utils.RRException;
import com.shenyi.shop.api.IOrderService;
import com.shenyi.shop.dao.OrderMapper;
import com.shenyi.shop.dao.ShippingMapper;
import com.shenyi.shop.entity.OrderDO;
import com.shenyi.shop.entity.ShippingDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author richard.fan
 * @version 1.0
 * @date 2018/11/05
 * @desc 订单
 * @see
 * @since 1.0
 */
@Service
public class OrderServiceImpl implements IOrderService {


    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShippingMapper shippingMapper;

    @Override
    public OrderDO queryObject(Integer id) {
        return orderMapper.queryObject(id);
    }

    @Override
    public List<OrderDO> queryList(Map<String, Object> map) {
        return orderMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return orderMapper.queryTotal(map);
    }

    @Override
    public int save(OrderDO order) {
        return orderMapper.save(order);
    }

    @Override
    public int update(OrderDO order) {
        return orderMapper.update(order);
    }

    @Override
    public int delete(Integer id) {
        return orderMapper.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return orderMapper.deleteBatch(ids);
    }

    @Override
    public int confirm(Integer id) {
        OrderDO OrderDO = queryObject(id);
        Integer shippingStatus = OrderDO.getShippingStatus();//发货状态
        Integer payStatus = OrderDO.getPayStatus();//付款状态
        if (2 != payStatus) {
            throw new RRException("此订单未付款，不能确认收货！");
        }
        if (4 == shippingStatus) {
            throw new RRException("此订单处于退货状态，不能确认收货！");
        }
        if (0 == shippingStatus) {
            throw new RRException("此订单未发货，不能确认收货！");
        }
        OrderDO.setShippingStatus(2);
        OrderDO.setOrderStatus(301);
        return orderMapper.update(OrderDO);
    }

    @Override
    public int sendGoods(OrderDO order) {
        Integer payStatus = order.getPayStatus();//付款状态
        if (2 != payStatus) {
            throw new RRException("此订单未付款！");
        }

        ShippingDO ShippingDO = shippingMapper.queryObject(order.getShippingId());
        if (null != ShippingDO) {
            order.setShippingName(ShippingDO.getName());
        }
        order.setOrderStatus(300);//订单已发货
        order.setShippingStatus(1);//已发货
        return orderMapper.update(order);
    }
}
